package seekting.demo2017.ripple;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;

import seekting.demo2017.R;

/**
 * Created by Administrator on 2017/9/14.
 */

public class RippleCompatButton extends android.support.v7.widget.AppCompatButton implements RippleAble {

    private MaterialRippleHolder materialRippleHolder = null;


    private static final int style;

    static {
        style = isLow() ? R.attr.rippleCompatLayoutStyle : R.attr.buttonStyle;

    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        if (isLow()) {
            materialRippleHolder.setTargetClickListener(l);
        } else {
            super.setOnClickListener(l);
        }

    }

    public RippleCompatButton(Context context) {
        this(context, null, 0);
    }

    public RippleCompatButton(Context context, AttributeSet attrs) {
        this(context, attrs, style);
    }

    public RippleCompatButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (isLow()) {
            materialRippleHolder = new MaterialRippleHolder();
            materialRippleHolder.init(context, attrs, defStyle, this);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean superOnTouchEvent = super.onTouchEvent(event);
        if (isLow()) {
            return materialRippleHolder.onTouchEvent(event, superOnTouchEvent, isEnabled());
        }
        return superOnTouchEvent;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (isLow()) {
            materialRippleHolder.onSizeChanged(w, h, oldw, oldh);
        }

    }

    @Override
    public boolean isInEditMode() {
        return false;
    }

    @Override
    public void draw(Canvas canvas) {
        if (isLow()) {
            materialRippleHolder.draw(canvas);
        } else {
            super.draw(canvas);
        }

    }

    @Override
    public void viewDraw(Canvas canvas) {
        if (isLow()) {
            super.draw(canvas);
        }
    }

    public static boolean isLow() {
        return false | Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
    }
}
